package da.metricscraft.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import da.metricscraft.MetricsCraft;

public class XMLWriter {
	public static class XMLData{
		public XMLData(String attribute, String value){
			this.attribute = attribute;
			this.value = value;
		}
		public XMLData(String attribute, String value, String text){
			this.attribute = attribute;
			this.value = value;
			this.text = text;
		}
		public String text = "";
		public String attribute;
		public String value;
	}
	private static boolean writeToNode(Document doc, ArrayList<XMLData> elementAttributeList, Node nl, String elementName) {
		Element currentElement = (Element)nl;
		boolean returnValue = false;
		for(XMLData entry: elementAttributeList){
			if (!entry.attribute.equals("") && !entry.value.equals(""))
				currentElement.setAttribute(entry.attribute, entry.value);
			if (!entry.text.equals("")){
				currentElement.setTextContent(entry.text);
			}
		}
		return returnValue;
	}
	
	public static void write(Document doc, HashMap<String, ArrayList<XMLData>> elementAttributeList, int searchThroughId, String elementName){
		Element writingElement = doc.getElementById(searchThroughId + "");
		boolean created = false;
		if(writingElement == null){
			created = true;
			writingElement = doc.createElement(elementName);
			writingElement.setAttribute("id", searchThroughId + "");
			writingElement.setIdAttribute("id", true);
		}
		for (Entry<String, ArrayList<XMLData>> entry : elementAttributeList.entrySet()){
			Element e = doc.createElement(entry.getKey());
			writeToNode(doc, entry.getValue(), e, entry.getKey());
			writingElement.appendChild(e);
		}

		if (created) doc.getFirstChild().appendChild(writingElement);
	}
	public static void write(Document doc, HashMap<String, ArrayList<XMLData>> elementAttributeList, String searchThroughTag, String searchName, String elementName){
		write(doc, elementAttributeList, searchThroughTag, searchName, elementName, false);
	}
	public static void write(Document doc, HashMap<String, ArrayList<XMLData>> elementAttributeList, String searchThroughTag, String searchName, String elementName, boolean append){
		Element writingElement = getElementByName(doc, searchThroughTag, searchName);
		boolean elementCreated = false;
		if (writingElement == null){
			writingElement = doc.createElement(searchThroughTag);
			writingElement.setAttribute("name", searchName);
			elementCreated = true;
		}else if (append){
			try{
				doc.getFirstChild().removeChild(writingElement);
			}catch (DOMException ex){
				MetricsCraft.log.info("[XMLWriter][write] Error removing Child: " + ex.getMessage());
			}
			writingElement = doc.createElement(searchThroughTag);
			writingElement.setAttribute("name", searchName);
			elementCreated = true;
		}
		for (Entry<String, ArrayList<XMLData>> entry : elementAttributeList.entrySet()){
			Element e = doc.createElement(entry.getKey());
			writeToNode(doc, entry.getValue(), e, entry.getKey());
			writingElement.appendChild(e);
		}
		if (elementCreated) doc.getFirstChild().appendChild(writingElement);
	}

	
	private static Element getElementByName(Document doc, String searchThroughTag, String elementName) {
		Element returnvalue = null;
		if (searchThroughTag != null){
			NodeList nl = doc.getElementsByTagName(searchThroughTag);
			for (int i = 0; i < nl.getLength(); i++){
				if (nl.item(i).getAttributes().getNamedItem("name").getTextContent().equalsIgnoreCase(elementName)){
					returnvalue = (Element)nl.item(i);
					break;
				}
			}
		}
		return returnvalue;
	}
}
